unit main;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  Grids, ExtCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    btnOpenGraph: TButton;
    btnCalculate: TButton;
    dlOpenFile: TOpenDialog;
    imgGraph: TImage;
    mOuput: TMemo;
    pbGraph: TPaintBox;
    sgViewData: TStringGrid;
    procedure btnCalculateClick(Sender: TObject);
    procedure btnOpenGraphClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure pbGraphPaint(Sender: TObject);
  private
    { private declarations }
    fUnvisited: array of Boolean;
    fCount: Integer;
    fGraph: TBitmap;

    procedure DrawGraph();
    procedure DFSRecursive(v: Integer);
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.DrawGraph();
var
  i, j, w, h: Integer;
begin
  with fGraph.Canvas do
  begin
		  Brush.Color := RGBToColor(207, 234, 246); //$CFEAF6;
		  Brush.Style := bsSolid;
		  FillRect(Rect(0, 0, Width, Height));
  end;

  w := 10;
  h := 10; //fGraph.Height div 2;
  for i := 0 to self.fCount - 1 do
  begin
    with fGraph.Canvas do
    begin
      Brush.Color := RGBToColor(255, 50, 0);
      Ellipse(Rect(w, h, 40, h + 30));
      Pen.Color := RGBToColor(0, 0, 0);
      TextOut(25, h + 15, IntToStr(i + 1));
    end;

    w := w + 100;
    h := h + 100;
    {
    for j := i + 1 to self.fCount - 1 do
    begin
      if StrToInt(sgViewData.Cells[i, j]) <> 0 then
      begin
         with fGraph.Canvas do
         begin
            Brush.Color := RGBToColor(255, 50, 0);
            Ellipse(Rect(w, h, 40, h + 30));
            Pen.Color := RGBToColor(0, 0, 0);
            TextOut(25, h + 15, IntToStr(j));
         end;
      end;
    end;
    }
  end;

  pbGraph.Invalidate;
end;

procedure TForm1.DFSRecursive(v: Integer);
var
  i: Integer;
begin
    self.fUnvisited[v] := True;
    for i := 1 to self.fCount do
    begin
        if StrToInt(sgViewData.Cells[i, v]) <> 0 then
            if not self.fUnvisited[i] then
                DFSRecursive(i);
    end;

    mOuput.Append(IntToStr(v) + ' <-- ');
end;

procedure TForm1.btnOpenGraphClick(Sender: TObject);
var
  ls, ls1: TStringList;
  n, i, m, j: Integer;
  s: String;
begin
     if dlOpenFile.Execute then
     begin
        ls := TStringList.Create;
        ls.LoadFromFile(dlOpenFile.FileName);
        n := ls.Count;
        sgViewData.RowCount := n + 1;
        sgViewData.ColCount := n + 1;
        for i := 1 to n do
        begin
             sgViewData.Cols[0][i] := IntToStr(i);
             sgViewData.Rows[0][i] := IntToStr(i);

             s := ls[i - 1];
             s := StringReplace(s, ' ', #13#10, [rfReplaceAll]);
             ls1 := TStringList.Create;
             ls1.Text := s;
             m := ls1.Count - 1;
             for j := 0 to m do
             begin
                  sgViewData.Rows[i][j + 1] := ls1[j];
             end;
        end;

        self.fCount := n;
        SetLength(self.fUnvisited, n);
        for i := 0 to n - 1 do
          self.fUnvisited[i] := False;

        DrawGraph();

        ls.Free;
        ls1.Free;
     end;
end;

procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  //FreeAndNil(fGraph);
  //FreeAndNil(fUnvisited);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  fGraph := Tbitmap.Create;
  with fGraph do
  begin
      PixelFormat := pf32bit;
      Width := pbGraph.Width;
      Height := pbGraph.Height;
      with Canvas do
      begin
		        Brush.Color := $FFFFFF;
		        Brush.Style := bsSolid;
		        FillRect(Rect(0, 0, Width, Height));

      end;
  end;

  pbGraph.Invalidate;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  //
end;

procedure TForm1.pbGraphPaint(Sender: TObject);
begin
  pbGraph.Canvas.Draw(0, 0, fGraph);
end;

procedure TForm1.btnCalculateClick(Sender: TObject);
begin
    DFSRecursive(1);
end;

end.

